using UnityEngine;
using System.Collections;

/// \class  gkSpawnTriggerByTime
/// \brief  Make is owner spawn a random number of actor at regular scrolling distance
[AddComponentMenu("GK/Engine/Game/Spawn/gkSpawnTriggerByTime")]
public class gkSpawnTriggerByTime : gkSpawnTrigger 
{
	/// The average time between each spawn
	public float AverageSpawnPeriod = 1.0f;
	
	/// The period variation in percent
	public float PeriodVariationPercent;
		
	/// The Current distance to wait between spawn
	private float m_fCountDownTimeBeforeNextSpawn;
	
	/// \brief  Update the spawning process
	protected override void UpdateSpawn()
	{
		// Update the spawn timer
		CountDownTimeBeforeNextSpawn();
		
		base.UpdateSpawn();
	}
	
	/// \brief  Spawn the actors
	protected override void SpawnActors()
	{
		base.SpawnActors();
	
		// Select the next time to wait before next spawn
		ResetCountDownTimeBeforNextSpawn();
	}
	
	/// \brief  Count Down Time Before Next Spawn
	void CountDownTimeBeforeNextSpawn()
	{
		m_fCountDownTimeBeforeNextSpawn -= Time.deltaTime;
	}
	
	/// \brief  Select the next time to wait before next spawn
	void ResetCountDownTimeBeforNextSpawn()
	{
		float fRandomPeriodVariationPercent;
	
		// Apply random variation to have the distance of the next spawn
		fRandomPeriodVariationPercent = Random.Range(-PeriodVariationPercent, PeriodVariationPercent) ;
		m_fCountDownTimeBeforeNextSpawn = AverageSpawnPeriod * (1.0f + fRandomPeriodVariationPercent);
	}
	
	/// \brief  Can we spawn again
	protected override bool CanSpawn()
	{
		return m_fCountDownTimeBeforeNextSpawn <= 0.0f;
	}
}
